var pages=sessionStorage.getItem("pages")
var carouselSettingsID=sessionStorage.getItem("carouselSettingsID")
if (pages==2){
    //查看
    $.ajax({
        type: "POST",
        url: root.brandWebsite.searchDetail,
        cache: false,  //禁用缓存
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify({
            "id":carouselSettingsID,
        }),
        success: function (data) {
            $("#title").val(data.data.title)
                $("#myImg").attr("src",data.data.pictrueUrl)
            $("#adPcUrl").val(data.data.pictureContentUrl)
            $("#pictureSort").val(data.data.pictureSort)

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });

}


//网站
querybrandWebsites()
function querybrandWebsites() {
    $.ajax({
        type: "POST",
        url: root.brandWebsite.query,
        cache: false,  //禁用缓存
        dataType: "json",
        async:false,//优先运行
        contentType: "application/json",
        data: JSON.stringify({

        }),
        success: function (data) {
            for (var i=0;i<data.data.data.length;i++){
                $("#websiteId").append("<option value=" + data.data.data[i].id + " >" + data.data.data[i].websiteName + "</option>")
            }

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

//图片上传
function addBtn(cen) {
    $(cen).prev().trigger('click');
}
//图片上传验证
var formData = new FormData();
// 提交上传图片
function getupfilerul(can,e){
    var formData = new FormData();
    formData.append('file',$(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            for(var i=0; i<data.length; i++){
                $(can).parent().parent().parent().find('img').attr("src", data[0].fullPath);
                $(can).parent().find('.fileupone').html('更换图片')
                $("#fileUpone").attr('type','text');
                $("#fileUpone").attr('type','file');
            }

        }
    })
}
// 上传图片返现
function putDataToPage(e,can) {
    //验证
    verificationPicFileType(can)
    verificationPicFileSize(can)
    for(var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if(!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src= data;
            img.onload = function(){
                var width = img.width;
                var height = img.height;
                console.log(height)
                console.log(width)
                if (Number(width) == Number(1920) && Number(height) == Number(660)){
                    getupfilerul(can,e)

                }else {
                    $.Huimodalalert('请上传正确格式图片 ',2000);
                    file.value = "";
                    $("#fileUpone").attr('type','text');
                    $("#fileUpone").attr('type','file');
                    return;

                }
                getupfilerul(can,e)

            };


        }
    }
    formData.append('file', $(can)[0].files[0]);
}
// 图片类型验证
function verificationPicFileType(file) {
    var flag=false;
    var fileTypes = [".jpg", ".png",".jpeg"];
    var filePath = file.value;
    //当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
    if(filePath){
        var isNext = false;
        var fileEnd = filePath.substring(filePath.indexOf("."));
        for (var i = 0; i < fileTypes.length; i++) {
            if (fileTypes[i] == fileEnd) {
                isNext = true;
                flag= true;
                break;
            }
        }
        if (!isNext){
            $.Huimodalalert('请上传正确格式图片',2000);
            file.value = "";
            flag=false;
        }
    }
    return flag;
}
//图片大小验证
function verificationPicFileSize(file) {
    var flag=false;
    var fileSize = 0;
    var fileMaxSize = 5*1024*1024;
    var filePath = file.value;
    if(filePath){
        fileSize =file.files[0].size;
        var size = fileSize / Number(1024);
        if (size > fileMaxSize) {
            $.Huimodalalert('上传图片过大，请压缩大小后再试',2000);
            file.value = "";
            flag=false;
        }else{
            flag=true;
        }
    }
    return flag;
}
//删除图片
function deletfilefns(e){
    $('#fileupones').html('图片上传')
    $(e).parent().parent().parent().find('img').attr('src','')
    $("#fileUpone").attr('type','text');
    $("#fileUpone").attr('type','file');

}

//限制字符

//保存
function preservation() {
    console.log($("#myImg").attr("src"))
    if($("#myImg").attr("src")==""){
        layer.msg('请上传图片!', {icon: 5, time: 2000});
        return false;
    }
    if ($("#title").val()==""||$("#myImg").attr("src")==""||$("#pictureSort").val()==""){
        $.Huimodalalert('请完善必填信息',2000);
        return false;
    }
    if (pages==2){

        $.ajax({
            type: "POST",
            url: root.brandWebsite.update,
            cache: false,  //禁用缓存
            dataType: "json",
            async:false,//优先运行
            contentType: "application/json",
            data: JSON.stringify({
                "id":carouselSettingsID,
                "websiteId":$("#websiteId").val(),
                "title":$("#title").val(),
                "pictrueUrl":$("#myImg").attr("src"),
                "pictureContentUrl":$("#adPcUrl").val(),
                "pictureSort":$("#pictureSort").val(),
                "createUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
            }),
            success: function (data) {
                if(data.errorCode==0){
                    layer.msg("保存成功",{icon: 6,time:2000},function () {
                        setTimeout(function () {
                            window.parent.querybrandWebsitessd();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        })
                    });
                }else  if(data.errorCode==5){
                    $.Huimodalalert(data.sub_msg,2000);
                }else {
                    $.Huimodalalert(data.msg,2000);
                }
            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });

    }else {
        $.ajax({
            type: "POST",
            url: root.brandWebsite.save,
            cache: false,  //禁用缓存
            dataType: "json",
            async:false,//优先运行
            contentType: "application/json",
            data: JSON.stringify({
                "websiteId":$("#websiteId").val(),
                "title":$("#title").val(),
                "pictrueUrl":$("#myImg").attr("src"),
                "pictureContentUrl":$("#adPcUrl").val(),
                "pictureSort":$("#pictureSort").val(),
                "createUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name
            }),
            success: function (data) {
                if(data.errorCode==0){
                    layer.msg("保存成功",{icon: 6,time:2000},function () {
                        setTimeout(function () {
                            window.parent.querybrandWebsitessd();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        })
                    });
                }else  if(data.errorCode==5){
                    $.Huimodalalert(data.sub_msg,2000);
                }else {
                    $.Huimodalalert(data.msg,2000);
                }
            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
    }


}